/**
 * 武汉天瑞兴科技有限公司
 */
package com.adds.lvds.security.acegi.session;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.adds.lvds.security.acegi.utils.CurrentUserUtils;



/**
 *项目名称 ：lvds-web-security
 *@author zhangjian
 *@time 2017-11-23上午10:11:35
 */
public class SessionTimeoutInterceptor implements HandlerInterceptor{
	private List<String> allowUrls = new ArrayList<String>();  
    public List<String> getAllowUrls() {  
        return allowUrls;  
    }  
    public void setAllowUrls(List<String> allowUrls) {  
        this.allowUrls = allowUrls;  
    }  
  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,   
            Object handler) throws Exception {  
        String requestUrl = request.getRequestURI();  
        for(String url : allowUrls) {  
            if(requestUrl.endsWith(url)) {  
                return true;  
            }  
        }  
        String session = CurrentUserUtils.currentUser(request).getUserName();  
        if("".equals(session)) {  
        	response.sendRedirect(request.getSession().getServletContext().getContextPath()+"/sessionTimeout");
            throw new SessionTimeoutException();  
        } else {  
            return true;  
        }  
    }  
    @Override  
    public void postHandle(HttpServletRequest request, HttpServletResponse response,   
            Object handler, ModelAndView modelAndView) throws Exception {  
          
    }  
    @Override  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,   
            Object handler, Exception ex) throws Exception {  
          
    }  
}
